package com.example.glidelearningproject.fragment.drawableTypeRequest;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.bumptech.glide.GenericTransitionOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.example.glidelearningproject.GlideApp;
import com.example.glidelearningproject.R;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import butterknife.BindView;
import butterknife.OnClick;

@Page(name = "Glide图片加载方式")
public class LoadTypeFragment extends XPageFragment {
    @BindView(R.id.typeSpinner)
    Spinner typeSpinner;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.btnOK)
    Button btnOK;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_load_type;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {

    }

    @OnClick(R.id.btnOK)
    protected void onClick(View view){
        String type = typeSpinner.getSelectedItem().toString();
        int position = typeSpinner.getSelectedItemPosition();
        switch (position) {
            case 0:
                Glide.with(getContext()).load("http://img2.3lian.com/2014/f6/173/d/51.jpg").into(imageView);
                break;
            case 1:
                Glide.with(getContext()).load(R.mipmap.jpg001).into(imageView);
                break;
            case 2:
                File file = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES) + File.separator,"hand.jpg");
                Glide.with(getContext()).load(file).into(imageView);
                break;
            case 3:
                File uriFile = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES) + File.separator + "25516019-b66dd039e3e365a5.webp");
                Uri uri = Uri.fromFile(uriFile);
                Glide.with(getActivity()).load(uri).into(imageView);
                break;
            case 4:
                try {
                    URL url = new URL("https://upload-images.jianshu.io/upload_images/4612461-cef217a76ec3cb99.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/676/format/webp");
                    Glide.with(getActivity()).load(url).into(imageView);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                break;
            case 5:
                Glide.with(getContext()).load("http://img2.3lian.com/2014/f6/173/d/51.jpg")
                        .placeholder(R.mipmap.placeholder_vertical)
                        .transition(withCrossFade())
                        .into(imageView);


                break;
            default:
                break;
        }
    }
}
